const app = getApp()
var Util = require('../../utils/util.js')
import * as utils from '../../utils/util.js';
Page({
  data: {
    mask_view: true,//遮罩层
    getUserInfoDialog: true,//获取用户信息
    getUserNumberDialog: true,//获取用户手机号
    saveQRCodeDialog: true,//保存二维码
    headerImg:'',//用户头像
  },
  onShow(){
    let that = this;
    that.setData({
      mask_view: false,
      getUserInfoDialog: false,
      getUserNumberDialog: true,
      saveQRCodeDialog: true,//保存二维码
    })
  },
    //授权功能，以及获取手机号
    getUserInfo: function (res) {
      let that = this;
      if (res.detail.errMsg === "getUserInfo:fail auth deny") {
        return;
      } else {
        let userInfo = JSON.parse(res.detail.rawData);
        //根据openid登陆
        this.loginByOpenid(userInfo, res.detail.encryptedData, res.detail.iv);
      }
    },
    loginByOpenid(catchUserInfo, catchUIEncryptedData, catchUIIv) {
      //根据openid登陆
      wx.setStorageSync('catchUserInfo', catchUserInfo);
      wx.setStorageSync('catchUIEncryptedData', catchUIEncryptedData);
      wx.setStorageSync('catchUIIv', catchUIIv);
      Util.wxLogin((res) => {
        if (res.data.code == 500) {//未登录，手机号授权
          let unid = res.data.message;
          wx.setStorageSync('unionId', unid);
          this.setData({
            mask_view: true,
            getUserInfoDialog: true,
            getUserNumberDialog: false,
          })
          wx.setStorageSync('isLogin', false);
          app.globalData.isLogin = false;
        } else {//正常登陆，直接登陆
          wx.setStorageSync("telNum", res.data.data.account);
          wx.setStorageSync('usertoken', res.data.data.a_token);
          wx.setStorageSync('isLogin', true);
          app.globalData.isLogin = true;
          let unionId = wx.getStorageSync('unionId');
          let userInfo = wx.getStorageSync('catchUserInfo');
          console.log(userInfo)
          //获取微信通知数据
          let wxNotice = wx.getStorageSync("userInfo").wxNotice
          let img = wx.getStorageSync("catchUserInfo").avatarUrl
          this.setData({
            mask_view: true,
            getUserInfoDialog: true,
            getUserNumberDialog: true,
            saveQRCodeDialog: true,
            headerImg: img,
          })
          if (unionId){
            app.globalData.gio('identify', res.data.data.xcxOpenid, unionid);
            app.globalData.gio('setVisitor', userInfo)
          }
          wx.navigateBack()
        }
      })
    },
    getPhoneNumber: function (res) {
      var that = this;
      if (res.detail.errMsg == "getPhoneNumber:fail user deny") {//用户拒绝获取手机号
        that.setData({
          mask_view: false,
          getUserNumberDialog: false,
          saveQRCodeDialog: true,
        });
        return;
      } else {
        //用户同意授权
        let img = wx.getStorageSync("userInfo").avatar
        that.setData({
          mask_view: false,
          getUserNumberDialog: true,
          saveQRCodeDialog: false,
          headerImg: img,
        });
        wx.setStorageSync('cashIv', res.detail.iv);
        wx.setStorageSync('cashEncryptedData', res.detail.encryptedData);
        //自动登录
        Util.loginByMobile((res) => {
          if (res.data.code == 200) {
            wx.setStorageSync("telNum", res.data.data.account);
            wx.setStorageSync('usertoken', res.data.data.a_token)
            wx.setStorageSync('isLogin', true)
            app.globalData.isLogin = true;
            let unionId = wx.getStorageSync('unionId');
            let userInfo = wx.getStorageSync('catchUserInfo');
            app.globalData.gio('identify', res.data.data.xcxOpenid, unionId);
            app.globalData.gio('setVisitor', userInfo);
            wx.navigateBack()
          } else {
            wx.showToast({
              title: res.data.message,
            })
          }
        })
      }
    },
    //关闭弹框
    closesaveQRCode() {
      this.setData({
        mask_view: true,
        saveQRCodeDialog: true
      })
      wx.navigateBack()
    },
    //保存二维码
    saveQRCode() {
      new Promise((resolve,reject)=>{
        wx.downloadFile({
          url: 'https://hq-prd-ymxt.oss-cn-szfinance.aliyuncs.com/upload/store/pt/2020/04/903585d4-362a-4ec3-8b4d-ee6fd3cea1c1.png?x-oss-process=style/YS11200',     //仅为示例，并非真实的资源
          success: function (res) {
            // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
            if (res.statusCode === 200) {
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                  wx.showToast({
                    title: '保存图片成功！',
                  })
                  resolve(res)
                },
                fail(res) {
                  reject(res)
                  wx.showToast({
                    title: '保存图片失败！',
                  })
                }
              })
            }
          }
        })
      }).then(res=>{
        this.setData({
          mask_view: true,
          saveQRCodeDialog: true
        })
        wx.navigateBack()
      }).catch(err=>{
        this.setData({
          mask_view: false,
          saveQRCodeDialog: false
        })
      })

    },
    closeLogin() {
      // wx.navigateTo({
      //   url: '/pages/index/index',
      // })
      // 检测是否登录
      if (this.data.source && this.data.source != "") {
        wx.navigateTo({
          url: '/pages/index/index',
        })
      }else {
        // wx.navigateBack()
        wx/wx.switchTab({
          url: '/pages/index/index',
        })
      }
    }
})
